<template>
    <div class="m-box">
        <hint :title="hint.title" :content="hint.content"></hint>
        <h2>后台系统设置</h2>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="300px" class="demo-ruleForm">
            <el-form-item label="网站名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="网站LOGO">
                <el-upload
                        class="avatar-uploader"
                        :action="adminApi('upload-img/1')"
                        :data="csrf_token"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="网站备案号" prop="caseNumber">
                <el-input v-model="ruleForm.caseNumber"></el-input>
            </el-form-item>
            <el-form-item label="联系人" prop="linkman">
                <el-input v-model="ruleForm.linkman"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="tel">
                <el-input v-model="ruleForm.tel"></el-input>
            </el-form-item>
            <el-form-item label="联系地址" prop="address">
                <el-input v-model="ruleForm.address"></el-input>
            </el-form-item>
            <el-form-item label="网站开关" prop="openDown">
                <el-switch v-model="ruleForm.openDown"></el-switch>
                <span class="c-danger">网站关闭时文字必须填写</span>
            </el-form-item>
            <el-form-item label="关闭网站文字显示" prop="downDesc">
                <el-input type="textarea" v-model="ruleForm.downDesc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                hint: {
                    title: '操作提示',
                    content: '网站全局设置，基本配置'
                },
                title: 'web_set',
                ruleForm: {
                    name: '',
                    caseNumber: '',
                    linkman: '',
                    tel: '',
                    address: '',
                    openDown: true,
                    downDesc: ''
                },
                imageUrl: '',
                path: '',
                csrf_token: {_token: $('meta[name="csrf-token"]').attr('content')},
                rules: {
                    name: [{required: true, message: '请输入网站名称', trigger: 'blur'}],
                    // openDown: [{ required: true, message: '请选择网站开关' }],
                    // downDesc: [{ required: true, message: '请输网站关闭时文字显示', trigger: 'blur'}],
                }
            }
        },
        created() {
            this.getWebSet();
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.ruleForm.openDown === false) {
                            if (this.ruleForm.downDesc === '') {
                                this.$message('关闭网站请填写文字显示');
                                return false;
                            }
                            this.saveWebSet();
                        } else {
                            this.saveWebSet();
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            getWebSet() {
                axios.post(this.adminApi('/config-page'), {
                    'title': this.title
                }).then(res => {
                    if (res.data.code === 0) {
                        // this.$message.success(res.data.msg)
                        this.ruleForm.name = res.data.info.webName
                        this.imageUrl = res.data.info.logoPath
                        this.ruleForm.caseNumber = res.data.info.caseNumber
                        this.ruleForm.linkman = res.data.info.linkman
                        this.ruleForm.tel = res.data.info.tel
                        this.ruleForm.address = res.data.info.address
                        res.data.info.openDown === 1 ? this.ruleForm.openDown=true : this.ruleForm.openDown=false
                        this.ruleForm.downDesc = res.data.info.downDesc
                    } else {
                        this.$message.error(res.data.msg)
                    }
                }).catch(res => {
                    this.$message.error('请稍后重试1！')
                })
            },
            //保存网站基本设置
            saveWebSet() {
                let img_path = '';
                if (this.path) {
                    img_path = this.path;
                } else {
                    img_path = this.imageUrl
                }
                axios.post(this.adminApi('/web-set'), {
                    title: this.title,
                    name: this.ruleForm.name,
                    path: img_path,
                    caseNumber: this.ruleForm.caseNumber,
                    linkman: this.ruleForm.linkman,
                    tel: this.ruleForm.tel,
                    address: this.ruleForm.address,
                    openDown: this.ruleForm.openDown ? 1 : 0,
                    downDesc: this.ruleForm.downDesc
                }).then(res => {
                    if (res.data.code === 0) {
                        this.$message.success(res.data.msg)
                        this.$router.push({'path':'/setting-site'})
                    } else {
                        this.$message.error(res.data.msg)
                    }
                }).catch(res => {
                    this.$message.error('请稍后重试2！');
                })

            },
            //服务器上传图片
            handleAvatarSuccess(res, file) {
                console.log(res)
                this.imageUrl = URL.createObjectURL(file.raw);
                if (res.code === 0) {
                    this.$message.success(res.msg);
                    this.path = res.path;
                } else {
                    this.$message.err(res.msg);
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>